package com.app.demo.activitys;


import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.app.demo.MainActivity;
import com.app.demo.R;
import com.app.shop.mylibrary.base.BaseActivity;
import com.app.shop.mylibrary.beans.EventMessage;
import com.app.shop.mylibrary.beans.UserBean;
import com.app.shop.mylibrary.utils.SharedPreferencesUtil;
import com.app.shop.mylibrary.utils.StringUtil;
import com.app.shop.mylibrary.utils.ToastUtil;
import com.app.shop.mylibrary.utils.UserManager;

import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.inputName)
    EditText inputName;
    @BindView(R.id.inputpwd)
    EditText inputpwd;
    @BindView(R.id.imgv_return)
    ImageView imgvReturn;

    EditText inputVerifCode;
    TextView TxVerifCode;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        imgvReturn.setVisibility(View.GONE);
        tvTitle.setText("登录");
        inputVerifCode=(EditText) findViewById(R.id.inputVerifCode);
        TxVerifCode=(TextView) findViewById(R.id.TxVerifCode);
        TxVerifCode.setText(genVerifCode());
        TxVerifCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TxVerifCode.setText(genVerifCode());
            }
        });
    }


    @OnClick({R.id.imgv_return, R.id.toLogin, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.toLogin:

                if (StringUtil.isEmpty(inputName.getText().toString())) {
                    ToastUtil.showToast(this, "请输入学号");
                    return;
                }

                if (StringUtil.isEmpty(inputpwd.getText().toString())) {
                    ToastUtil.showToast(this, "请输入密码");
                    return;
                }

                if (StringUtil.isEmpty(inputVerifCode.getText().toString())) {
                    ToastUtil.showToast(this, "请输入验证码");
                    return;
                }

                if(!checkStringEqual(inputVerifCode.getText().toString(),TxVerifCode.getText().toString())){
                    ToastUtil.showToast(this, "验证码错误");
                    TxVerifCode.setText(genVerifCode());
                    return ;
                }
                boolean isHaveUser = UserManager.isHaveUser(inputName.getText().toString());
                if (isHaveUser) {//有该用户
                    if (UserManager.isOk(inputName.getText().toString(), inputpwd.getText().toString())) { //密码正确
                        UserBean userBean = UserManager.getUser(inputName.getText().toString());
                        SharedPreferencesUtil.saveDataBean(this, userBean, "user");
                        EventBus.getDefault().post(new EventMessage(EventMessage.LOGIN));
                        ToastUtil.showToast(this, "登录成功");
                        showActivity(this, MainActivity.class);
                        finish();
                    } else {
                        ToastUtil.showToast(this, "密码不匹配");
                    }
                } else {
                    ToastUtil.showToast(this, "无此用户，请先注册");
                }


                break;

            case R.id.tv_register:
                Intent intent = new Intent(this, RegisterActivity.class);
                showActivity(this, intent);
                break;
        }
    }

    public static boolean checkStringEqual(String str1,String str2){
        String tmp1=str1.toLowerCase();
        String tmp2=str2.toLowerCase();
        return tmp2.equals(tmp1);
    }
    public static String genRandomNumber(){
        String[] alphabet=new String[]{"0","1","2","3","4","5","6","7","8","9",
                "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
        };
        Random random=new Random();
        int num=random.nextInt(62);
        return alphabet[num];
    }

    public static String genVerifCode(){
        String VerifyCode="";
        for(int i=0;i<4;++i){
            String tmp=genRandomNumber();
            VerifyCode=VerifyCode+tmp;
        }
        return VerifyCode;
    }
}
